package com.cxf.windex.rest;

import com.cxf.windex.base.msg.TableResultResponse;
import com.cxf.windex.base.rest.BaseRestController;
import com.cxf.windex.biz.PlatformUserBiz;
import com.cxf.windex.entity.CxfPlatform;
import com.cxf.windex.entity.PlatformUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Api(tags = {"聚能小宝 - 用户接口"})
@Controller
@RequestMapping("api_platform_user")
public class PlatformUserController extends BaseRestController<PlatformUserBiz, PlatformUser> {

  @ApiOperation(value = "获取平台用户列表", notes = "获取平台用户列表")
  @RequestMapping(value = "/list", method = RequestMethod.GET)
  @ResponseBody
  public TableResultResponse<PlatformUser> list(@RequestParam(defaultValue = "10") int limit,
      @RequestParam(defaultValue = "1") int offset,
      @RequestParam(defaultValue = "desc") String orderStr, String name, String phone,
      String platformName) {
    Example example = new Example(PlatformUser.class);
    Example.Criteria criteria = example.createCriteria();
    String type = this.getCurrentUserType();
    if (!type.equals("4")) {
      String userid=getCurrentUserId();
      criteria.andEqualTo("crtUser",userid );
    }
    if (StringUtils.isNotBlank(name)) {
      criteria.andLike("name", "%" + name + "%");
    }
    if (StringUtils.isNotBlank(phone)) {
      criteria.andLike("username", "%" + phone + "%");
    }
    if (StringUtils.isNotBlank(platformName)) {
      criteria.andLike("platformName", "%" + platformName + "%");
    }

    example.setOrderByClause("crt_time  desc");
    PageHelper.startPage(offset, limit);
    List<PlatformUser> items = baseBiz.selectByExample(example);

    PageInfo<PlatformUser> pageInfo = new PageInfo<PlatformUser>(items);
    return new TableResultResponse<PlatformUser>(
        Integer.parseInt(String.valueOf(pageInfo.getTotal())),
        offset, limit,
        items);
  }

  @ApiOperation(value = "获取平台用户列表", notes = "获取平台用户列表")
  @RequestMapping(value = "/unpayList", method = RequestMethod.GET)
  @ResponseBody
  public TableResultResponse<PlatformUser> unpayList(@RequestParam(defaultValue = "10") int limit,
      @RequestParam(defaultValue = "1") int offset,
      @RequestParam(defaultValue = "desc") String orderStr, String name, String phone,
      String platformName) {
    PageHelper.startPage(offset, limit);
    List<PlatformUser> items = baseBiz.selectUnpay(name,phone,platformName);

    PageInfo<PlatformUser> pageInfo = new PageInfo<PlatformUser>(items);
    return new TableResultResponse<PlatformUser>(
        Integer.parseInt(String.valueOf(pageInfo.getTotal())),
        offset, limit,
        items);
  }
}
